package day01;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class t1 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 创建线程的第三种方式
        // 第一种
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("我是thread1创建方法");
            }
        });
        t1.start();


        // 第二种方式
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("我是thread2创建方法");
            }
        };
        Thread t2 = new Thread(runnable, "thread2");
        t2.start();

        // 第三种方式
        FutureTask<Integer> task = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                System.out.println("我是thread3创建方法");
                Thread.sleep(5000);
                return 100;
            }
        });
        Thread t3 = new Thread(task, "thread3");
        t3.start();
        // task.get()会阻塞拿到返回值或者抛出异常后才会往下面执行
        System.out.println(task.get());
        System.out.println("我已经获取到返回值了");
    }
}
